<template>
  <div>
    <div class="hearder-search">
      <div class="toplineBox mb-10">
        <div class="toplineBox-left">
          <el-input v-model="editForm.name" placeholder="填写任务名称" size="small" class="w-140 mr-10"></el-input>
          <!--            <el-input v-model="editForm.realname" placeholder="发布人" size="small" class="w-140 mr-10"></el-input>-->
          <!--            <el-input v-model="editForm.name" placeholder="执行人" size="small" class="w-140 mr-10"></el-input>-->
          <el-select v-model="editForm.is_finish" placeholder="选择状态" size="small"
                     class="w-140 mr-10">
            <el-option :label="item.name" :value="item.id" v-for="(item, index) in sectionList"
                       :key="index"></el-option>
          </el-select>
          <el-select v-model="editForm.type" placeholder="选择状态" size="small"
                     class="w-140 mr-10">
            <el-option :label="item.name" :value="item.id" v-for="(item, index) in timeList"
                       :key="index"></el-option>
          </el-select>
          <!--// 执行中 完成-->
          <!--周报表 月报表 年报表 近7天 30天 365天-->
          <!--去除时间到分-->
          <el-button type="primary" size="small" @click="searchFn"><i class="el-icon-search"></i>搜索</el-button>
          <el-button size="small" @click="refreshFn"><i class="el-icon-refresh"></i>刷新</el-button>
          <downexcel type="teskList"></downexcel>
        </div>
        <p class="numberline toplineBox-right">总{{ page.total }}条</p>
      </div>
    </div>
    <div class="table-body">
      <div class="table-body-box">
        <el-table
            ref="multipleTable"
            :data="arryList"
            :stripe="tableStyle.tableStripe"
            :border="tableStyle.tableBorder"
            tooltip-effect="dark"
            style="width: 100%"
            header-cell-class-name="table-text-c"
            cell-class-name="table-text-c">
          <el-table-column
              prop="name"
              label="任务名">
          </el-table-column>
          <el-table-column
              label="发布人"
              width="100">
            <template slot-scope="scope">
              {{ scope.row.user.realname }}
            </template>
          </el-table-column>
          <el-table-column
              prop="create_time"
              label="下发时间"
              width="160">
          </el-table-column>
          <el-table-column
              prop="end_time"
              label="限改时间"
              width="110">
          </el-table-column>
          <el-table-column
              prop="complete_time"
              label="完成时间"
              width="110">
          </el-table-column>
          <!--// 24小时 一天计算-->
          <!--            <el-table-column-->
          <!--                    prop="time"-->
          <!--                    label="超时记录"-->
          <!--                    width="160">-->
          <!--            </el-table-column>-->
          <!--顺序排序-->
          <el-table-column
              prop="end_time"
              label="任务状态"
              width="80">
            <template slot-scope="scope">
                <span
                    :class="scope.row.is_finish==1?'c-success':'c-danger'">{{
                    scope.row.is_finish == 1 ? '完成' : '执行中'
                  }}</span>
            </template>
          </el-table-column>
          <el-table-column
              label="操作"
              width="280">
            <template slot-scope="scope">
              <el-button size="small" @click="handleEdit(scope.row.id)">执行情况</el-button>
              <el-button type="primary" size="small" @click="taskDetail(scope.row.id, 'detail')" plain>任务详情
              </el-button>
              <el-button type="danger" size="small" @click="handleDetel(scope.$index)" v-if="userInfoid==1"
                         plain>删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!--工具条 分页-->
    <el-col :span="24" class="toolbar">
      <el-pagination
          background
          layout="prev, pager, next"
          :total="page.total"
          :page-size="page.pagesize"
          @current-change="handleSizeChange">
      </el-pagination>
    </el-col>
    <taskDetailDialog :visible.sync="taskDetailvisible" v-if="taskDetailvisible"
                      :editBoxId="editBox" @InitFn="InitFn"></taskDetailDialog>
    <drawerTask @checkTask="checkTask" :visible.sync="dialogDrawerVisible" :taskId="taskId"
                v-if="dialogDrawerVisible"></drawerTask>
<!--          <detailPoupe></detailPoupe>-->
  </div>
</template>

<script>
import drawerTask from '@/components/dialog/drawerTask.vue'
import taskDetailDialog from '@/components/dialog/taskDetailDialog.vue'
import {pagination, tableoption, hourTime} from '@/utils/util.js'
import detailPoupe from "../detailPoupe";
import {mapGetters} from "vuex";

export default {
  components: {
    drawerTask,
    taskDetailDialog,
    detailPoupe
  },
  data() {
    return {
      dialogDrawerVisible: false, // 右侧滑窗状态
      arryList: [], // 页面数据列表
      taskDetailvisible: false, // 任务
      // 搜索设置
      searchWord: '', // 搜索名称
      // 设置table样式
      tableStyle: tableoption,
      // 设置table页面分页所需
      page: {
        pageindex: pagination.pageIndex,
        pagesize: pagination.pageSize,
        total: pagination.total
      },
      editBox: null,
      clickStatus: null,
      taskId: null,
      editForm: {
        name: '',
        is_finish: '',
        p: '',
        type: ''
      },
      timeList: [
        {
          name: '全部',
          id: ''
        },
        {
          name: '最近一周',
          id: 1
        },
        {
          name: '最近一月',
          id: 2
        }
      ],
      sectionList: [
        {
          name: '全部',
          id: ''
        },
        {
          name: '执行中',
          id: 0
        },
        {
          name: '完成',
          id: 1
        }
      ],
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  created() {
    this.InitFn()
  },
  methods: {
    // 初始化
    InitFn() {
      this.editForm.p = this.page.pageindex
      this.$fetch(this.api.task_index, this.editForm).then((response) => {
        if (response.code === 0) {
          let list_ = response.list.data
          for (let i in response.list.data) {
            let data_ = response.list.data[i].create_time.split(' ')
            let data_l = data_[1].split(':')
            data_l.splice(2, 1)
            data_[1] = data_l.join(':')
            response.list.data[i].create_time = data_.join(' ')
            // scope.row.is_finish==1?'完成':'执行中'
            if (response.list.data[i].is_finish === 1) {
              if (response.list.data[i].time > 0) {
                response.list.data[i].time = hourTime(response.list.data[i].time)
              } else {
                response.list.data[i].time = '未超时'
              }
            } else {
              response.list.data[i].complete_time = '未完成'
              response.list.data[i].time = '未完成'
            }
          }
          this.arryList = response.list.data
          this.page.total = response.list.total
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    },
    searchFn() {
      this.page.pageindex = 1
      this.InitFn()
    },
    // 编辑 执行情况
    handleEdit(id) {
      this.taskId = id
      this.dialogDrawerVisible = true
    },
    // 任务详情 数据获取
    taskDetail(id, type, name) {
      this.clickStatus = type
      this.editBox = id
      this.taskDetailvisible = true
    },
    // 刷新页面
    refreshFn() {
      this.InitFn()
    },
    // 设置默认选中
    checked(option) {
      if (option === 'first' && JSON.stringify(this.applyBookBox) !== '{}') {
        this.applyBookBox = {}
        this.$refs.table.clearSelection()
      }
      for (let i = 0; i < this.userList.length; i++) {
        if (this.applyBookBox[this.userList[i].id]) {
          this.$refs.table.toggleRowSelection(this.userList[i], true)
        }
      }
    },
    // 分页
    handleSizeChange(val) {
      this.page.pageindex = val
      this.InitFn()
    },
    // 刷新详情信息
    getUser() {
      this.taskDetail(this.editBox, 'refreshFn')
    },
    // 弹窗
    checkTask() {

    },
    handleDetel(index) {
      this.$confirm('确认永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.detelRespon(index)
      }).catch(() => {
      })
    },
    detelRespon(index) {
      this.$post(this.api.task_delete, {id: this.arryList[index].id}).then((response) => {
        if (response.code === 0) {
          this.arryList.splice(index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        } else {
        }
      }).catch((err) => {
        console.log(err)
      })
    }
  },
  mounted() {
  }
}
</script>

<style lang="scss">
.evaluateBox {
  background: #eee;
  margin-top: 5px;
  border-radius: 4px;
  padding: 5px;
}

.lookLsit {
  cursor: pointer;
}

.lookLsit:hover {
  text-decoration: underline;
}

#tablebox {
  display: flex;
  flex-direction: column;
}

.el-table {
  color: #000;
}

.right-box .container .main .content-container .input-val-box .input-top {
  width: 250px;
}

</style>
